package com.eve.controller.admin;

import com.eve.entity.Picture;
import com.eve.entity.User;
import com.eve.service.PictureService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.security.PublicKey;
import java.sql.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping("/admin")
@Log4j2
public class PictureController {
    @Autowired
    private PictureService pictureService;

    @Value("${file-save-path}")
    private String fileSavePath;
    /**
     * 展示所有文件信息
     */
    @GetMapping("/showpicture")
    public String showPicture(HttpSession session, ModelMap modelMap,@RequestParam(defaultValue = "1",value = "pageNum") Integer pageNum){
        /**
         * 在登陆的session中拿到用户信息
         */
        User user = (User)session.getAttribute("user");
//        System.out.println(user);
        PageHelper.startPage(pageNum,7);
        //根据用户信息查询所有文件信息
        List<Picture> PictureList = pictureService.findByUserId(user.getId());
        PageInfo<Picture> pageInfo = new PageInfo<Picture>(PictureList);
        //存入作用域
        modelMap.put("pageInfo",pageInfo);
//        System.out.println("---pictkjnfdjk------"+pageInfo);
        return "admin/picture";
    }

    /**
     * 上传图片并把信息保存到数据库中‘
     * (Person person, @RequestParam("file") MultipartFile file
     */
    @PostMapping("/upload")
    /**
     * 图片保存路径
     */
    public  String upload(@RequestParam("pictureaddress") MultipartFile file,
                          Integer id, String picturename, String picturedescription,
                          HttpSession session, Model model) throws IOException {
        //获取上传文件用户的id
        User user =(User) session.getAttribute("user");

        String oldFileName =file.getOriginalFilename();

        //存放指定路径，产生一个新文件夹
//        File newFile =new File(fileSavePath,oldFileName);

        //5.创建 sun 公司提供的 jersey 包中的 Client 对象
        Client client = Client.create();
        //6.指定上传文件的地址，该地址是 web 路径
        WebResource resource = client.resource(fileSavePath+oldFileName);
        //7.实现上传
        String result = resource.put(String.class,file.getBytes());
        System.out.println(result);


        //将文件信息放入数据库中
        String sqlPath =fileSavePath +"/"+ oldFileName;
        Picture picture =new Picture();
        picture.setPictureaddress(sqlPath)
                .setUserid(user.getId())
                .setPicturename(picturename)
//                .setPicturetime(new Date())
                .setPicturedescription(picturedescription);
        pictureService.save(picture);
        model.addAttribute("PictureLists",picture);
        return "redirect:/admin/showpicture";
    }

        //根据id删除数据
    @GetMapping("/picturedelete")
    public  String deletepicture(Integer id){
        pictureService.deletePicture(id);
        return "redirect:/admin/showpicture";
    }

}
